 <%@include file="/WEB-INF/common/taglibs.jsp"%>
<script type="text/javascript" src="${ctx}/resources/js/datatable-query.js"></script>
<div class="row-fluid">
		<div class="span12">
			<div class="box gradient">
				<div class="title">
					<h4><span>User</span>
						<span class="box-form right">
							<a href="#" id="del"class="btn reset btn-mini">Reset Filter</a>
	<a href="#" id="del"class="btn btn-mini">Del</a>
	<a href="#" id="lock" class="btn btn-mini">Lock</a>
	<a href="#" id="not-lock" class="btn btn-mini">Unlock</a>
	<a href="${ctx}/security/user/create/"  class="btn btn-mini">Create</a>


						</span>
					</h4>
					<a href="#" class="minimize">Minimize</a>
				</div>
				<div class="content clearfix scrollable">
					<table class="ajaxTable display table table-bordered">
						<thead>
							<tr>
								<th><a id="select_all"><span class="icomoon-icon-checkmark-2"></span></a></th>
 								<th>UserName </th>
								<th>LoginName</th>
								<th>Email</th>
								<th>Active </th>
								<th>UnLock </th>
								<th>Actions</th>
							</tr>
						</thead>
					</table>
				</div>
			</div>
		</div>
	</div>


<script type="text/javascript">

var render = false;
var oTime = "";
var oTable;
$(document).ready(function () {
	oTable = $('.ajaxTable').dataTable({
		"bLengthChange": true,
		"aoColumnDefs": [
		  { 'bSortable': false, 'aTargets': [0,6] }
		],
		"sAjaxSource": '${ctx}/security/user/',
		"fnInitComplete": function (oSettings, json) { }
	});
	var name = $("<input>").attr({
		type: "text",
		field: "name",
		op: "cn",
		placeholder: "UserName",
		class: "filterdata"
	});
	var searchLabel = $("<label>").html("Search:").css("display", "inline");
	var filterDiv = $("<div>").addClass("dataTables_filter").append(searchLabel).append(name);
	$('.dataTables_wrapper').prepend(filterDiv);

	
	johnny.resetFilter($('.reset'));

	$(".datepickerlazy").datepicker({
		dateFormat: 'yy-mm-dd',
		onSelect: function () {
			updateGrid();
		}
	}).keydown(function (event) {
		if (event.keyCode == 8) { $(".datepickerlazy").val(""); }
		else { event.preventDefault(); }
	});

	$(".filterdata").keyup(function () {
		updateGrid();
	});
});
function updateGrid() {
	window.clearTimeout(oTime);
	oTime = setTimeout(function () { oTable.fnDraw(); }, 500);
}

var columnHeader = ['','name', 'loginName', 'email','actived'];
var QueryData = function (aoData) {
	return johnny.initQueryData(aoData, columnHeader);
}

var DataTableForm = function (json, echo) {
	var dataArray = new Array();
	for (var i = 0; i < json.result.length; i++) {
		var subArray = new Array();
		var value = johnny.getForSelect(json.result[i].id);
 		subArray.push(value);
 		subArray.push(json.result[i].name);
		subArray.push(json.result[i].loginName);
		subArray.push(json.result[i].email);
		subArray.push(json.result[i].actived);
		subArray.push(json.result[i].accountNonLocked);
		subArray.push(actionHtml(json.result[i].id));
		dataArray.push(subArray);
	}
	return {
		sEcho: echo,
		iTotalRecords: json.totalCount,
		iTotalDisplayRecords: json.totalCount,
		aaData: dataArray
	};
}


function actionHtml(id) {
return	johnny.getEditHtml("${ctx}/security/user/"+id+"/edit/");
}
</script>
 
 





<script type="text/javascript">
	$(function(){
		
		$("#evict-cache").click(function(){
			var items = johnny.select();
			if(items && items.count <=0){
				alert("Clean cache");
				return false;
			}
			if(items && items.length > 0 && confirm("Confirm to clean cache?")){ 
				var itemsAsString = items.join(",");
				$.ajax({
					url : "${ctx}/security/user/evict-cache/",
					type : "POST",
					data : {"_method" : "PUT", "items" : itemsAsString},
					dataType : "json",
					success : function(data){
						if(data && data.state && (data.state.indexOf("ERROR") != -1)){
							alert(data.message);
							return;
						}
						
						updateGrid();
					}
				});
				
				return false;
			}
		});
			
		$("#lock").click(function(){
			var items = johnny.select();
			
			if(items && items.length <=0){
				johnny.warning("Please select at least one user for opertaion");
				return false;
			}
			
			if(confirm("Confirm to lock the selected user?")){
				$.ajax({
					url : "${ctx}/security/user/lock/",
					type : "POST",
					data : {"_method" : "PUT", "ids" : items},
					dataType : "json",
					success : function(data){
						if(data && data.state && (data.state.indexOf("ERROR") != -1)){
							alert(data.message);
							return;
						}
						johnny.success("Lock user successfully");
						updateGrid();
					}
				});
				
				return false;
			}
			
			return false;
		});
		
		$("#not-lock").click(function(){
			var items = johnny.select();
			
			if(items && items.length <=0){
				johnny.warning("Please select at least one user for opertaion");
				return false;
			}
			if(confirm("Confirm to unlock the selected user?")){
				$.ajax({
					url : "${ctx}/security/user/not-lock/",
					type : "POST",
					data : {"_method" : "PUT", "ids" : items},
					dataType : "json",
					success : function(data){
						if(data && data.state && (data.state.indexOf("ERROR") != -1)){
						johnny.warning(data.message);
							return;
						}
						
						johnny.success("Unlock user successfully");
						updateGrid();
					}
				});
				
				return false;
			}
		});
		
		$("#del").click(function(){
			var items = johnny.select();
			
			if(items && items.count <=0){
				johnny.warning("Please select at least one user for opertaion");
				return false;
			}
			
			if(confirm("Confirm to delete the selected user?")){
				
				$('input[name="_method"]').remove();
				$("#myForm").attr("action", "${ctx}/security/user/delete/")
							.attr("method","post")
							.append('<input type="hidden" name="_method" value="DELETE" />')
							.submit();
				//No more action after adding this sentence
				return false;
			}
		});
	});
</script>
 
<body>

